package com.coin900.coin.cmd.user;

import com.google.gson.Gson;
import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.code.RespCode;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.service.fu.IFuUserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取用户
 *
 * @author shui
 * @create 2017-11-30
 **/
@Controller
public class FuUserGetCmd extends ACmd {

    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        String source = sign.getSource();
        JSONObject jsonObject = JSONObject.fromObject(source);
        long id = jsonObject.getLong("id");
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("dataStatus", 1);
        List<FuUserVO> fuUserVOS = this.userService.loadFuUser(params);
        if (fuUserVOS.isEmpty()) {
            respSign.setRespCode(RespCode.FAIL);
            respSign.setMsg("用户不存在");
            return respSign;
        }

        FuUserVO userVO = fuUserVOS.get(0);
        source = new Gson().toJson(userVO);
        respSign.setSource(source);
        return respSign;
    }

    @Autowired
    private IFuUserService userService;

    @Override
    public String getCmdCode() {
        return CmdCode.FU_USER_GET;
    }
}
